
package ui.activity.poscontrol.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import event.HasPushEvent;
import ui.activity.poscontrol.beanmodel.PushBackPeopleBean;


/**
 * POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class PushBackRecordOtherPeopleAdapter extends DelegateAdapter.Adapter<PushBackRecordOtherPeopleAdapter.ProfitVH> {

    private final Context mContext;
    private List<PushBackPeopleBean> datas = new ArrayList<>();

    PushBackPosAdapter adapter;
    public PushBackRecordOtherPeopleAdapter(Context context, List<PushBackPeopleBean> data) {
        mContext = context;
        datas = data;
    }

    public void setData(List<PushBackPeopleBean> data) {
        datas = data;
        if(null!=adapter) {
            adapter.notifyDataSetChanged();
        }
        notifyDataSetChanged();
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public ProfitVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ProfitVH(LayoutInflater.from(mContext).inflate(R.layout.layout_pushback_flowedpeople, viewGroup, false));
    }


    @Override
    public void onBindViewHolder(@NonNull ProfitVH holder, int position) {
        if (position < 0 || position >= datas.size()) {
            return;
        }
        String name = datas.get(position).getRealName();
        String phone = datas.get(position).getPhoneNumber();
        phone = phone.substring(0,3)+"****"+phone.substring(7,11);
        holder.custom.setText(name);
        holder.posnum.setText(datas.get(position).getCount()+"台");
        holder.flow_custom_phone.setText(phone);

        if(datas.get(position).getPosRecords().size()>0&& datas.get(position).isSelected()){
            holder.device_list.setVisibility(View.VISIBLE);
            holder.device_list.setLayoutManager(new LinearLayoutManager(mContext));
            holder.pushback_title_linear.setVisibility(View.VISIBLE);
            adapter  = new PushBackPosAdapter(mContext,datas.get(position).getPosRecords(),position+"");
            holder.device_list.setAdapter(adapter);
            holder.customArrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.arrow_up_blue));
        }else{
            holder.pushback_title_linear.setVisibility(View.GONE);
            holder.device_list.setVisibility(View.GONE);
            holder.customArrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.arrow_down_333333));

        }   holder.flow_custom_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datas.get(position).setSelected(!holder.flow_custom_head.isSelected());
                holder.flow_custom_head.setSelected(!holder.flow_custom_head.isSelected());
                if(holder.flow_custom_head.isSelected()){
                    holder.pushback_title_linear.setVisibility(View.VISIBLE);
                    holder.customArrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.arrow_up_blue));
                    holder.device_list.setVisibility(View.VISIBLE);
                }else{
                    holder.pushback_title_linear.setVisibility(View.GONE);
                    holder.customArrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.arrow_down_333333));
                    holder.device_list.setVisibility(View.GONE);
                }
                if(datas.get(position).getPosRecords().size()<1) {
                    EventBus.getDefault().post(new HasPushEvent(datas.get(position).getFromId(), "", ""));
                }
            }
        });
        holder.youxiaoqi_linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( holder.youxiaoqi_image.isSelected()){
                    holder.youxiaoqi_image.setSelected(false);
                    holder.youxiaoqi_image.setBackgroundResource(R.mipmap.topsearch);
                    EventBus.getDefault().post(new HasPushEvent(datas.get(position).getFromId(),"expire_start_time","") );
                }else{
                    holder.youxiaoqi_image.setSelected(true);
                    holder.youxiaoqi_image.setBackgroundResource(R.mipmap.downsearch);
                    EventBus.getDefault().post(new HasPushEvent(datas.get(position).getFromId(),"","expire_start_time") );
                }

            }
        });
        holder.piliangchuli_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( !holder.piliangchuli_text.isSelected()){
                    holder.piliangchuli_text.setSelected(true);
                    holder.piliangchuli_text.setBackgroundResource(R.mipmap.morechangeyes);
                    TobuyApplication.piliangPosition.add(position+"");
                }else{
                    holder.piliangchuli_text.setSelected(false);
                    holder.piliangchuli_text.setBackgroundResource(R.mipmap.morechangeno);
                    TobuyApplication.piliangPosition.remove(position+"");
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ProfitVH extends RecyclerView.ViewHolder {
        TextView custom;
        ImageView customArrow;
        RelativeLayout flow_custom_head;
        TextView posnum;
        TextView flow_custom_phone;
        RecyclerView device_list;
        LinearLayout pushback_title_linear;//下方标题栏
        LinearLayout youxiaoqi_linear;//有效期linear
        ImageView youxiaoqi_image;//有效期的imageview
        TextView piliangchuli_text;//排序的按钮

        ProfitVH(@NonNull View itemView) {
            super(itemView);
            device_list = itemView.findViewById(R.id.device_list);
            posnum = itemView.findViewById(R.id.peoplehasposnum);
            custom = itemView.findViewById(R.id.flow_custom_name);
            flow_custom_phone = itemView.findViewById(R.id.flow_custom_phone);
            customArrow = itemView.findViewById(R.id.flow_expand_arrow);
            flow_custom_head = itemView.findViewById(R.id.flow_custom_head);
            pushback_title_linear = itemView.findViewById(R.id.pushback_title_linear);
            youxiaoqi_linear = itemView.findViewById(R.id.youxiaoqi_linear);
            youxiaoqi_image = itemView.findViewById(R.id.youxiaoqi_image);
            piliangchuli_text = itemView.findViewById(R.id.piliangchuli_text);


        }
    }
}
